﻿Option Strict On
Option Infer On
Option Explicit On

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.StoredProcModel
Imports GenDotNet.Task.Common
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">

Public Class RelationKeyData
   Inherits BaseData
   Implements IRelationKeyData

   Private mChildColumnName As String
   Private mParentColumnName As String
   Private mChildTableName As String
   Private mParentTableName As String
   Private mChildAlias As String
   Private mParentAlias As String

   Public Shared Function Load( _
            ByVal rootdata As RootData, _
            ByVal elements As IEnumerable(Of XElement)) _
            As IEnumerable(Of IRelationKeyData)
      Dim list = New List(Of IRelationKeyData)
      For Each e In elements
         list.Add(New RelationKeyData(rootdata, e))
      Next
      Return list
   End Function

   Private Sub New( _
            ByVal rootdata As RootData, _
            ByVal element As XElement)
      MyBase.New(rootdata, element)
      If element.Name.LocalName = "ChildKey" Then
         mChildColumnName = Me.Element.@ChildColumn
         mParentColumnName = Me.Element.@ParentColumn
         mChildTableName = Me.Element.@ChildTable
         mParentTableName = Me.Element.@ParentTable
         mChildAlias = Me.Element.@ChildAlias
         mParentAlias = Me.Element.@ParentAlias
      Else
         mChildColumnName = Me.Element.@RightColumn
         mParentColumnName = Me.Element.@LeftColumn
         Dim join = element.Ancestors(GetXmlNamespace(orm).GetName("Join")).FirstOrDefault
         mChildTableName = join.@RightOriginalName
         mParentTableName = join.@LeftOriginalName
         mChildAlias = join.@RightAlias
         mParentAlias = join.@leftAlias
      End If
   End Sub

   Public ReadOnly Property ChildColumnName() _
            As String _
            Implements IRelationKeyData.ChildColumnName
      Get
         Return mChildColumnName
      End Get
   End Property

   Public ReadOnly Property ChildTable() _
         As ITableData _
         Implements IRelationKeyData.ChildTable
      Get
         Return gettable(mChildTableName)
      End Get
   End Property

   Public ReadOnly Property ChildAlias() _
          As String _
          Implements IRelationKeyData.ChildAlias
      Get
         Return mChildAlias
      End Get
   End Property

   Public ReadOnly Property ParentColumnName() _
            As String _
            Implements IRelationKeyData.ParentColumnName
      Get
         Return mParentColumnName
      End Get
   End Property

   Public ReadOnly Property ParentTable() _
         As ITableData _
         Implements IRelationKeyData.ParentTable
      Get
         Return gettable(mParentTableName)
      End Get
   End Property

   Public ReadOnly Property ParentAlias() _
          As String _
          Implements IRelationKeyData.ParentAlias
      Get
         Return mParentAlias
      End Get
   End Property

End Class
